package com.design.movieticket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.entity.Balance;
import com.design.movieticket.entity.User;
import com.design.movieticket.entity.UserRole;
import com.design.movieticket.mapper.BalanceMapper;
import com.design.movieticket.mapper.UserMapper;
import com.design.movieticket.mapper.UserRoleMapper;
import com.design.movieticket.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private BalanceMapper balanceMapper;

    @Resource
    private UserService userService;

    /** 分页查询 */
    @Override
    public LambdaQueryWrapper buildQuery(User user){
        LambdaQueryWrapper query;
        if (user.getStatus() == 88) {
            query = Wrappers.lambdaQuery(User.class).orderByDesc(User::getId)
                    .eq(StringUtils.isNotBlank(user.getUsername()), User::getUsername, user.getUsername())
                    .in(User::getStatus, 0,1);
        }
        else{
            query = Wrappers.lambdaQuery(User.class)
                    .eq(StringUtils.isNotBlank(user.getUsername()), User::getUsername, user.getUsername())
                    .in(User::getStatus, user.getStatus());
        }
        return query;
    }

    /** 分页相关 */
    @Override
    public <E extends IPage<User>> E page(E page, Wrapper<User> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    /** 新增或更新用户数据 */
    @Override
    public void addOrUpdateUserInfo(User user) {
        if (user.getPassword() == null){
            user.setPassword(new BCryptPasswordEncoder().encode("123456"));
        }
        super.saveOrUpdate(user);
        if (userRoleMapper.selectOne(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId,user.getId())) == null){
            User user1 = userService.getUserInfoByUsername(user.getUsername()); //获取新建用户信息
            Long userId = user1.getId();

            UserRole userRole = new UserRole();
            userRole.setRoleId(4L);
            userRole.setUserId(userId);
            userRoleMapper.insert(userRole);

            Balance balance = new Balance(); //为新用户创建余额信息
            balance.setBalance(new BigDecimal(0));
            balance.setPoints(new BigDecimal(0));
            balance.setUsername(user.getUsername());
            balance.setUserId(userId);
            balanceMapper.insert(balance);
        }
    }

    /** 获取用户信息详情 */
    @Override
    public User getUserInfoDetail(long id) {
        return super.getById(id);
    }

    /** 删除用户信息 */
    @Override
    public void delUserInfoById(long id) {
        super.removeById(id);
    }


    @Override
    public User getUserInfoByUsername(String username) {
        System.out.println(username + "查询"+ super.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username)));
        return super.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
    }
}
